package cn.jdy.plugins.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.Info;

/**
 * @title : 我的消息实现类
 * @description :
 */
@Service
@Transactional
public class InfoService extends BaseDao<Info> {

	/**
	 * 我的消息记录数
	 * @param userId
	 * @param flag
	 * @return
	 */
	public String getMyInfoCount(String userId,String flag){
		Info info = new Info();
		info.setUserid(userId);
		info.setFlag(flag);
		return getInfoCount(info);//查询我的消息信息总条数
	}
	/**
	 * 查询我的消息信息总条数
	 *
	 * @return
	 */
	public String getInfoCount(Info bo) {
		try {
      return String.valueOf(count(createFilter(bo)));
		} catch(Exception e) {
			BaseLog.e(this.getClass(), " 查询我的消息信息总条数失败", e);
		}
		return null;
	}

	/**
	 * 查询我的消息信息
	 *
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Info> query(Info bo) {
		List<Info> list = null;
		try {
		  DetachedCriteria dc = createFilter(bo);
		  dc.addOrder(Order.desc("sendtime"));
      list =  find(dc,bo.getPagestart(),bo.getPagesize());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询我的消息信息异常", e);
		}
		return list;
	}
 
	/**
	 * 根据条件查询我的消息信息
	 *
	 * @param bo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Info> queryforList(Info bo) {
		List<Info> list = null;
		try {
			list = query( bo);
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询我的消息异常", e);
		}
		return list;
	}

	/**
	 * 保存配置
	 *
	 * @return String
	 */
  public void saveOrUpd(Info bo) {
    try {
      String flag = "0";
      flag = bo.getFlag() == "1" ? "1" : "0";
      bo.setFlag(flag);
      bo.setSendtime(DateUtil.getCurrentTime());
      save(bo);
    } catch (Exception e) {
      BaseLog.e(this.getClass(), "保存配置出错", e);
    }
  }

	private DetachedCriteria createFilter(Info bo){
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
    if(StringUtil.isNotEmpty(bo.getUserid())){
      dc.add(Restrictions.eq("userid", bo.getUserid()));
    }
    if(StringUtil.isNotEmpty(bo.getType())){
      dc.add(Restrictions.eq("type", bo.getType()));
    }
    if(StringUtil.isNotEmpty(bo.getSendtime())){
      dc.add(Restrictions.gt("sendtime", bo.getSendtime()));
    }
    if(StringUtil.isNotEmpty(bo.getFlag())){
      dc.add(Restrictions.eq("flag", bo.getFlag()));
    }
    if(StringUtil.isNotEmpty(bo.getStatus())){
      dc.add(Restrictions.eq("status", bo.getStatus()));
    }
    if(StringUtil.isNotEmpty(bo.getSendUserId())){
      dc.add(Restrictions.eq("sendUserId", bo.getSendUserId()));
    }
    if(StringUtil.isNotEmpty(bo.getBusinessid())){
      dc.add(Restrictions.eq("businessid", bo.getBusinessid()));
    }
    return dc;
	}
}
